package department

import (
	"context"
	"fmt"
	"net/http"
	"wms/model"

	"wms/internal/svc"
	"wms/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateLogic) Update(req *types.DepartmentUpdateRequest) (resp *types.BaseResponse, err error) {
	resp = new(types.BaseResponse)

	if req.Id == req.ParentId {
		fmt.Printf("[Error]不能将部门[%d]自身作为上级部门\n", req.Id)
		resp.Code = http.StatusBadRequest
		resp.Msg = "不能将部门自身作为上级部门"
		return resp, nil
	}

	//1.上级部门id是否存在
	var parent *model.Department
	parent, err = l.svcCtx.DepartmentModel.FindOne(l.ctx, req.ParentId)
	switch err {
	case nil:
		if parent.Type < req.Type {
			resp.Code = http.StatusBadRequest
			resp.Msg = "上级部门的类型不能低于下级部门"
			return resp, nil
		}
	case model.ErrNotFound:
		resp.Code = http.StatusBadRequest
		resp.Msg = "上级部门不存在"
		return resp, nil
	default:
		fmt.Printf("[Error]查询上级部门[%d]:%s\n", req.ParentId, err.Error())
		resp.Code = http.StatusBadRequest
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	//2.部门是否存在
	department, err := l.svcCtx.DepartmentModel.FindOne(l.ctx, req.Id)
	switch err {
	case nil:
		fmt.Printf("[Debug]部门[%d]\n", req.Id)
		if department.Type != req.Type {
			resp.Code = http.StatusBadRequest
			resp.Msg = "不支持修改部门类型"
			return resp, nil
		}
	case model.ErrNotFound:
		resp.Code = http.StatusBadRequest
		resp.Msg = "部门不存在"
		return resp, nil
	default:
		fmt.Printf("[Error]查询部门[%d]:%s\n", req.ParentId, err.Error())
		resp.Code = http.StatusBadRequest
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	//3.查询兄弟部门是否重复：name、full_name、code
	var ds = make([]model.Department, 0)
	ds, err = l.svcCtx.DepartmentModel.FindByPage(l.ctx, fmt.Sprintf(" where parent_id=%d ", req.ParentId))
	if err != nil {
		fmt.Printf("[Error]查询上级部门[%d]的下级:%s\n", req.ParentId, err.Error())
		resp.Code = http.StatusBadRequest
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	for _, d := range ds {
		if d.Id == req.Id { //忽略当前部门
			continue
		}

		if d.Name == req.Name {
			resp.Code = http.StatusBadRequest
			resp.Msg = "部门名称重复"
			return resp, nil
		}

		if d.FullName == req.FullName {
			resp.Code = http.StatusBadRequest
			resp.Msg = "部门全称重复"
			return resp, nil
		}

		if d.Code == req.Code {
			resp.Code = http.StatusBadRequest
			resp.Msg = "部门编码重复"
			return resp, nil
		}
	}

	//3.部门入库
	err = l.svcCtx.DepartmentModel.Update(l.ctx, &model.Department{
		Id:       req.Id,
		Type:     req.Type,
		SortId:   req.SortId,
		ParentId: req.ParentId,
		Name:     req.Name,
		FullName: req.FullName,
		Code:     req.Code,
		Remark:   req.Remark,
	})
	if err != nil {
		fmt.Printf("[Error]部门[%s]更新入库:%s\n", req.Name, err.Error())
		resp.Code = http.StatusBadRequest
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	resp.Code = http.StatusOK
	resp.Msg = "成功"
	return resp, nil
}
